package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IAppOrderService;
import com.mdd.admin.validate.AppOrderCreateValidate;
import com.mdd.admin.validate.AppOrderUpdateValidate;
import com.mdd.admin.validate.AppOrderSearchValidate;
import com.mdd.admin.vo.AppOrderListedVo;
import com.mdd.admin.vo.AppOrderDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.AppOrder;
import com.mdd.common.mapper.AppOrderMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 快递订单提交实现类
 * @author DEREK
 */
@Service
public class AppOrderServiceImpl implements IAppOrderService {
        
    @Resource
    AppOrderMapper appOrderMapper;

    /**
     * 快递订单提交列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<AppOrderListedVo>
     */
    @Override
    public PageResult<AppOrderListedVo> list(PageValidate pageValidate, AppOrderSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<AppOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        appOrderMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:username@userName:str",
            "=:ordernum@orderNum:str",
            "=:userid@userId:int",
            "like:recmanname@recManName:str",
            "=:recmanmobile@recManMobile:str",
            "=:recmanprintaddr@recManPrintAddr:str",
            "like:sendmanname@sendManName:str",
            "=:sendmanmobile@sendManMobile:str",
            "=:sendmanprintaddr@sendManPrintAddr:str",
            "like:cargoname@cargoName:str",
            "=:weight:str",
            "=:kuaidicom:str",
            "=:daytype@dayType:int",
            "=:pickupstarttime@pickupStartTime:str",
            "=:pickupendtime@pickupEndTime:str",
            "=:payprice@payPrice:str",
            "=:paystatus@payStatus:int",
            "=:paytype@payType:str",
            "=:payordernum@payOrderNum:str",
            "=:createtime@createTime:str",
            "=:paytime@payTime:str",
            "=:remark:str",
        });

        IPage<AppOrder> iPage = appOrderMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<AppOrderListedVo> list = new LinkedList<>();
        for(AppOrder item : iPage.getRecords()) {
            AppOrderListedVo vo = new AppOrderListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreatetime(TimeUtils.dateFormatDay(item.getCreatetime()));
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 快递订单提交详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return AppOrder
     */
    @Override
    public AppOrderDetailVo detail(Integer id) {
        AppOrder model = appOrderMapper.selectOne(
                new QueryWrapper<AppOrder>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        AppOrderDetailVo vo = new AppOrderDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 快递订单提交新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(AppOrderCreateValidate createValidate) {
        AppOrder model = new AppOrder();
        model.setUsername(createValidate.getUsername());
        model.setOrdernum(createValidate.getOrdernum());
        model.setUserid(createValidate.getUserid());
        model.setRecmanname(createValidate.getRecmanname());
        model.setRecmanmobile(createValidate.getRecmanmobile());
        model.setRecmanprintaddr(createValidate.getRecmanprintaddr());
        model.setSendmanname(createValidate.getSendmanname());
        model.setSendmanmobile(createValidate.getSendmanmobile());
        model.setSendmanprintaddr(createValidate.getSendmanprintaddr());
        model.setCargoname(createValidate.getCargoname());
        model.setWeight(createValidate.getWeight());
        model.setKuaidicom(createValidate.getKuaidicom());
        model.setDaytype(createValidate.getDaytype());
        model.setPickupstarttime(createValidate.getPickupstarttime());
        model.setPickupendtime(createValidate.getPickupendtime());
        model.setPayprice(createValidate.getPayprice());
        model.setPaystatus(createValidate.getPaystatus());
        model.setPaytype(createValidate.getPaytype());
        model.setPayordernum(createValidate.getPayordernum());
        model.setCreatetime(createValidate.getCreatetime());
        model.setPaytime(createValidate.getPaytime());
        model.setRemark(createValidate.getRemark());
        appOrderMapper.insert(model);
    }

    /**
     * 快递订单提交编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(AppOrderUpdateValidate updateValidate) {
        AppOrder model = appOrderMapper.selectOne(
                new QueryWrapper<AppOrder>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUsername(updateValidate.getUsername());
        model.setOrdernum(updateValidate.getOrdernum());
        model.setUserid(updateValidate.getUserid());
        model.setRecmanname(updateValidate.getRecmanname());
        model.setRecmanmobile(updateValidate.getRecmanmobile());
        model.setRecmanprintaddr(updateValidate.getRecmanprintaddr());
        model.setSendmanname(updateValidate.getSendmanname());
        model.setSendmanmobile(updateValidate.getSendmanmobile());
        model.setSendmanprintaddr(updateValidate.getSendmanprintaddr());
        model.setCargoname(updateValidate.getCargoname());
        model.setWeight(updateValidate.getWeight());
        model.setKuaidicom(updateValidate.getKuaidicom());
        model.setDaytype(updateValidate.getDaytype());
        model.setPickupstarttime(updateValidate.getPickupstarttime());
        model.setPickupendtime(updateValidate.getPickupendtime());
        model.setPayprice(updateValidate.getPayprice());
        model.setPaystatus(updateValidate.getPaystatus());
        model.setPaytype(updateValidate.getPaytype());
        model.setPayordernum(updateValidate.getPayordernum());
        model.setCreatetime(updateValidate.getCreatetime());
        model.setPaytime(updateValidate.getPaytime());
        model.setRemark(updateValidate.getRemark());
        appOrderMapper.updateById(model);
    }

    /**
     * 快递订单提交删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        AppOrder model = appOrderMapper.selectOne(
                new QueryWrapper<AppOrder>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        appOrderMapper.delete(new QueryWrapper<AppOrder>().eq("id", id));
    }

}
